package com.itypd.basic;

import com.itypd.pojo.VolatileTestObj;

/*
 * @Description: volatile知识点测试
 * @Athor: GongMingwei
 * @version: v1.0
 */
public class VolatileTest {
    public static void main(String[] args) {
        VolatileTestObj testObj = new VolatileTestObj();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                while(testObj.getI() < 999){
                    if(testObj.getFlag()){
                        testObj.setFlag(false);
                        testObj.setI(testObj.getI()+1);
                        System.out.println(testObj.getI());
                        testObj.setFlag(true);
                    }
                }
            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                while(testObj.getI() < 999){
                    if(testObj.getFlag()){
                        testObj.setFlag(false);
                        testObj.setI(testObj.getI()+1);
                        System.out.println(testObj.getI());
                        testObj.setFlag(true);
                    }
                }
            }
        });
        t1.start();
        t2.start();
    }
}
